package com.service.sys;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.DesUtils;
import com.common.StringUtils;
import com.common.db.RedisUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.common.model.UserTypeEnum;
import com.dao.supply.UserMapper;
import com.model.supply.SupplyUserModel;

/**
 * api
 * 
 * @author chenjun
 *
 */
@Service
public class ApiService {

	static final Logger logger = LoggerFactory.getLogger(ApiService.class);

	@Autowired
	UserMapper userMapper;
	@Autowired
	RedisUtils redisUtils;

	/**
	 * 获取token
	 *
	 * @return java.lang.String
	 * @author chenjun
	 * @date 2021/6/10 14:32
	 */
	public HttpResult login(Map<String, Object> params) {
		if (params.get("userAccount") == null || params.get("userPwd") == null) {
			return HttpResult.error("缺少参数！");
		}
		List<SupplyUserModel> list = userMapper.selectList(params);
		if (list.size() == 0) {
			return HttpResult.error("用户名密码不正确");
		}
//		long count = redisUtils.incr(UserTypeEnum.SupplyUser + "-count", 1);
//		redisUtils.expire(UserTypeEnum.SupplyUser + "-count", Global.PC_BACKGROUND_TOKEN_EXPIRE);
//		if (count > 30) {
//			return HttpResult.error("已超过次数！");
//		}
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		String token = "";
		try {
			token = DesUtils.encrypt(params.get("userAccount") + "," + System.currentTimeMillis() + ","
					+ UserTypeEnum.SupplyUser.getValue());
			String key = Global.REDIS_USER_PREFIX + UserTypeEnum.SupplyUser.getValue() + "-"
					+ params.get("userAccount");
			request.getSession().setAttribute("userType", UserTypeEnum.SupplyUser.getValue());
			request.getSession().setAttribute("supplyUserModel", list.get(0));
			redisUtils.set(key, list.get(0), Global.PC_BACKGROUND_TOKEN_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
		}

		HttpResult httpResult = new HttpResult();
		httpResult.setData(token);
		return httpResult;
	}

	/**
	 * 校验token
	 * 
	 * @param tokenString
	 * @return
	 */
	public boolean checkToken(String tokenString) {
		if (!StringUtils.isNotNull(tokenString)) {
			return false;
		}
		String desTokenString;
		try {
			desTokenString = DesUtils.decrypt(tokenString);
		} catch (IOException e) {
			return false;
		} catch (Exception e) {
			return false;
		}
		JSONObject jsonObject = JSON.parseObject(desTokenString);
		String userIdString = jsonObject.getString("userId");
		String string = redisUtils.get(UserTypeEnum.InterfaceUser + "-" + userIdString).toString();
		if (tokenString.equals(string)) {
			return true;
		} else {
			return false;
		}

	}
}
